package cn.jietuo.kafkaspringboot;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2021/4/11
 * @description: 消费者
 */
@Component
public class KafkaConsumer {

    @Value("${spring.kafka.template.default-topic}")
    private String topic;

    private final static String TOPIC_NAME = "test";

    @KafkaListener(topics = TOPIC_NAME, groupId = "test")
    public void listenGroup(ConsumerRecord<String, String> record, Acknowledgment ack) {
        System.out.println("直接在配置文件中写好topic：" + topic);
        String value = record.value();
        System.out.println("group1:" + value);
        System.out.println("group1:" + record);
        // 手动提交offset
        ack.acknowledge();
    }

    //配置多个消费组
    @KafkaListener(topics = TOPIC_NAME, groupId = "test1")
    public void listenTulingGroup(ConsumerRecord<String, String> record, Acknowledgment ack) {
        String value = record.value();
        System.out.println("group2:" + value);
        System.out.println("group2:" + record);
        ack.acknowledge();
    }
}
